package com.example.service.utils;

import com.example.entity.orders.OrderDetails;
import com.example.entity.shop.DamageReport;
import com.example.mapper.DamageReportMapper;
import com.example.mapper.OrderDetailsMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 库存推荐
 */
@Service
public class InventoryRecommendation {
    // 净销量
    // 安全系数
    @Resource
    private DamageReportMapper damageReportMapper;
    @Resource
    private OrderDetailsMapper orderDetailsMapper;

    public int inventoryQuantity(int damageReportId) {

        // 安全系数
        double safetyFactor = 1.2;
        // 报损数量
        DamageReport damageReport = damageReportMapper.damageReportCount(damageReportId);
        int lastWeekDamages = damageReport.getLossQuantity();
        // 上周销量
        OrderDetails damageReport1 = orderDetailsMapper.selectFinishSalesCountQuantity(damageReportId);
        int lastWeekSales = damageReport1.getTotalQuantity();
        // 上周推货量
        OrderDetails damageReport2 = orderDetailsMapper.selectReturnQuantity(damageReportId);
        int lastWeekReturns = damageReport2.getReturnQuantity();

        // TODO core algorithm
        int netSales = lastWeekSales - lastWeekReturns - lastWeekDamages;
        int safetyStock = (int) (netSales * safetyFactor);
        int recommendedStock = safetyStock + lastWeekDamages;

        return recommendedStock;
    }
}
